// 数据库操作类
#include <iostream>
#include <string>
#include <mysql.h>


//!todo 写配置文件
// 数据库配置信息
static std::string serverIP = "127.0.0.1";
static std::string userName = "root";
static std::string password = "123456";
static std::string dbname = "chat";

class MySQLTest
{
public:
    // 初始化数据库连接
    MySQLTest();
    // 释放数据库连接资源
    ~MySQLTest();
    // 连接数据库
    bool connect();

private:
    MYSQL *_conn;
};

MySQLTest::MySQLTest() {
    _conn = mysql_init(nullptr);
}

MySQLTest::~MySQLTest() {
    if (_conn != nullptr)
        mysql_close(_conn);
}

bool MySQLTest::connect() {
//    LOG_INFO<<"Connection::connect()";
    MYSQL *p = mysql_real_connect(_conn, serverIP.c_str(), userName.c_str(),
                                  password.c_str(), dbname.c_str(), 3306, nullptr, 0);
    if (p != nullptr){
        //设置编码，可用中文
        mysql_query(_conn, "set names gbk");
    }else{
        std::cout << "Connection connection failed: " << mysql_error(_conn);
    }
    return p;
}

int main(){
    MySQLTest mysqlTest;
    bool ok = mysqlTest.connect();
    std::cout<<"mysql连接:"<<ok;
}
